package com.example.laishichang.sqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import java.util.HashMap;

/**
 * Created by laishichang on 2018/3/21.
 */

public class ASDatabaseHelper extends SQLiteOpenHelper {


    //  SQLite信息：表名
    private static String tableName = "ReportDataCache";

    //  SQLite信息：表结构
    public static final String CREATE_BOOK = "create table ReportDataCache ("
            + "id integer primary key autoincrement, "
            + "timestamp text,"
            + "paramsJson text)";


    private Context mContext;
    public ASDatabaseHelper(Context context, String name, CursorFactory
            factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BOOK);
        Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }




    /**
     * 增
     * */
    public void insertReportData(String paramsJsonString) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        long time = System.currentTimeMillis()/1000; //获取系统时间的10位的时间戳
        String timestamp = String.valueOf(time);

        // 组装缓存信息， 并写入数据库中
        values.put("timestamp", timestamp);
        values.put("paramsJson", paramsJsonString);
        db.insert(tableName, null, values);
    }


    /**
     * 删
     * 删除第一条缓存信息
     * */
    public void deleteFirstReportData(String key) {
        if (key.length() > 0) {
            System.out.println("=======  delete  ======== key: " + key.toString());
            SQLiteDatabase db = this.getWritableDatabase();
            db.delete(tableName, "id = ?", new String[] { key });
        } else {
            System.out.println("=======  delete Fail  ======== key: " + key.toString());
        }

    }


    /**
     * 查
     * 获取第一条缓存信息
     * */
    public HashMap queryFirstReportData() {

        SQLiteDatabase db = this.getWritableDatabase();
        HashMap hashMap = new HashMap();

        // 查询Book表中所有的数据
        Cursor cursor = db.query(tableName, null, null, null, null, null, null);
        System.out.println("cursor.count is " + cursor.getCount());

        //  读取第一条信息
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String timestamp = cursor.getString(cursor.getColumnIndex("timestamp"));
            String paramsJson = cursor.getString(cursor.getColumnIndex("paramsJson"));

            //  拼接参数
            hashMap.put("id", id);
            hashMap.put("timestamp", timestamp);
            hashMap.put("paramsJson", paramsJson);
        }

        //  关闭数据库游标
        cursor.close();

        //  打印信息，并执行回调
        System.out.println("firstCache.hashMap is " + hashMap.toString());
        return hashMap;
    }


    /**
     * 获取数据库中缓存的数量
     * */
    public int getCacheDataCount() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.query(tableName, null, null, null, null, null, null);
        return cursor.getCount();
    }

}
